// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import func from "../config/func";
import config from "../config/config";
import AudioManage from "../manage/AudioManage";
import Guide from "../guide/Guide";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CookLevelUp extends cc.Component {

    @property({ type: cc.Node, tooltip: "背景框" })
    m_bg: cc.Node = null;
    @property({ type: cc.Sprite, tooltip: "厨师图片" })
    m_cook: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "厨师名字" })
    m_cookName: cc.Sprite = null;
    @property({ type: cc.SpriteFrame, tooltip: "厨师名字" })
    m_cookName_1: cc.SpriteFrame = null;
    @property({ type: cc.SpriteFrame, tooltip: "厨师名字" })
    m_cookName_2: cc.SpriteFrame = null;
    @property({ type: cc.Label, tooltip: "等级" })
    m_cookGrade: cc.Label = null;
    @property({ type: cc.Label, tooltip: "额外奖励数量" })
    m_additionMoney: cc.Label = null;
    @property({ type: cc.Label, tooltip: "加成时间" })
    m_additionTime: cc.Label = null;
    @property({ type: cc.Node, tooltip: "升级弹窗" })
    m_grade: cc.Node = null;
    @property({ type: cc.Label, tooltip: "升级所需货币" })
    m_btn_cont: cc.Label = null;

    n_cookInfo: cook = null;
    n_upMoney: number = 0;

    init(cookInfo: cook, refresh: boolean = false, spr: cc.SpriteFrame = null) {
        if (config.guide) {
            Guide.instance.hideGuide();
        }
        config.openPopup = false;
        this.n_cookInfo = cookInfo;
        if (!refresh) {
            func.setNodeAni(this.m_bg);
            this.m_cook.spriteFrame = spr;
            if (this.n_cookInfo.type == 1) {
                this.m_cookName.spriteFrame = this.m_cookName_1;
            } else {
                this.m_cookName.spriteFrame = this.m_cookName_2;
            }
        }
        this.m_cookGrade.string = `等级：${this.n_cookInfo.grade}`;
        this.m_additionMoney.string = `单份价格: +${this.n_cookInfo.additionMoney}`;
        if (this.n_cookInfo.type == 1) {
            this.m_additionTime.string = `切菜速度: +${this.n_cookInfo.additionTime}%`;
        } else {
            this.m_additionTime.string = `炒菜速度: +${this.n_cookInfo.additionTime}%`;
        }
        this.n_upMoney = 300 + this.n_cookInfo.additionMoney * 200;
        this.m_btn_cont.string = `${this.n_upMoney}`;
    }

    openGradeNode() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        this.m_grade.active = true;
    }

    closeGradeNode() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        this.m_grade.active = false;
    }
    /**
     * 升级厨师
     */
    upGrade() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (config.money >= this.n_upMoney) {
            config.money -= this.n_upMoney
            this.n_cookInfo.grade += 1;
            this.n_cookInfo.additionMoney += 1;
            this.n_cookInfo.additionTime += 10;
            if (this.n_cookInfo.type == 1) {
                config.cook_1 = this.n_cookInfo;
            } else {
                config.cook_2 = this.n_cookInfo;
            }
            this, this.m_grade.active = false;
            this.init(this.n_cookInfo, true);
        }
    }

    close() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        func.goReduceOut(this.m_bg, () => {
            this.node.active = false;
            if (config.guide) {
                config.guide = false;
                Guide.instance.showGuide(0, 0, cc.v2(), false);
            }
        })
    }
}
